package com.imooc.mybatisplusappdemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.mybatisplusappdemo.entity.Customer;
import com.imooc.mybatisplusappdemo.service.CustomerService;
import com.imooc.mybatisplusappdemo.util.ResultUtil;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author sanchongmen
 * @since 2021-03-22
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

  @Autowired
  private CustomerService customerService;

  /**
   * 进入列表页
   *
   * @return
   */
  @GetMapping("toList")
  public String toList() {
    return "customer/customerList";
  }

  @GetMapping("list")
  @ResponseBody
  public R<Map<String, Object>> list(String realName, String phone, long page, long limit) {

    LambdaQueryWrapper<Customer> queryWrapper = Wrappers.<Customer>lambdaQuery()
        .like(StringUtils.isNotBlank(realName), Customer::getRealName, realName)
        .like((StringUtils.isNotBlank(phone)), Customer::getPhone, phone)
        .orderByDesc(Customer::getCustomerId);

    Page<Customer> customerPage = customerService.page(new Page<>(page, limit), queryWrapper);

    //方式2
//    LambdaQueryChainWrapper<Customer> queryChainWrapper = customerService
//        .lambdaQuery()
//        .like(StringUtils.isNotBlank(realName), Customer::getRealName, realName)
//        .like((StringUtils.isNotBlank(phone)), Customer::getPhone, phone)
//        .orderByDesc(Customer::getCustomerId);

    return ResultUtil.buildPageR(customerPage);

  }

  /**
   * 进入新增页
   *
   * @return
   */
  @GetMapping("toAdd")
  public String toAdd() {
    return "customer/customerAdd";
  }

  @PostMapping
  @ResponseBody
  public R<Object> add(@RequestBody Customer customer) {
    return ResultUtil.buildR(customerService.save(customer));
  }

  /**
   * 进入修改页
   *
   * @return
   */
  @GetMapping("toUpdate/{id}")
  public String toUpdate(@PathVariable long id, Model model) {
    Customer customer = customerService.getById(id);
    model.addAttribute("customer", customer);

    return "customer/customerUpdate";
  }

  /**
   * 更新客户资料
   *
   * @param customer
   * @return
   */
  @PutMapping
  @ResponseBody
  public R<Object> update(@RequestBody Customer customer) {
    return ResultUtil.buildR(customerService.updateById(customer));
  }

  /**
   * 删除客户
   *
   * @param id
   * @return
   */
  @DeleteMapping("/{id}")
  @ResponseBody
  public R<Object> delete(@PathVariable long id) {
    return ResultUtil.buildR(customerService.removeById(id));
  }

  /**
   * 进入详情页
   *
   * @return
   */
  @GetMapping("toDetail/{id}")
  public String toDetail(@PathVariable long id, Model model) {
    Customer customer = customerService.getById(id);
    model.addAttribute("customer", customer);

    return "customer/customerDetail";
  }

  /**
   * 完成学习,后面的内容不上传代码,视频中已有
   */

}
